﻿namespace Sidvall.WorkItemManager.UI.Entities
{
    public sealed partial class Schedule
    {
        #region Public Members

        #region Description

        public string Description
        {
            get
            {
                return Sidvall.WorkItemManager.SystemContextConfig.Current.EntityFacade.ScheduleManager.GetScheduleDescription(this);
            }
        }

        #endregion
        #region MaxDayFrequency

        private byte _MaxDayFrequency;
        public byte MaxDayFrequency
        {
            get
            {
                return _MaxDayFrequency;
            }
            set
            {
                _MaxDayFrequency = value;
                OnPropertyChanged("MaxDayFrequency", true, false);
            }
        }

        #endregion
        #region MaxMonthFrequency

        private byte _MaxMonthFrequency;
        public byte MaxMonthFrequency
        {
            get
            {
                return _MaxMonthFrequency;
            }
            set
            {
                _MaxMonthFrequency = value;
                OnPropertyChanged("MaxMonthFrequency", true, false);
            }
        }

        #endregion
        #region MaxScheduleFrequency

        private byte _MaxScheduleFrequency;
        public byte MaxScheduleFrequency
        {
            get
            {
                return _MaxScheduleFrequency;
            }
            set
            {
                _MaxScheduleFrequency = value;
                OnPropertyChanged("MaxScheduleFrequency", true, false);
            }
        }

        #endregion
        #region NextExecutionTime

        public System.DateTime? NextExecutionTime
        {
            get
            {
                return GetNextExecutionTime(System.DateTime.Now, null);
            }
        }

        #endregion

        #region GetNextExecutionTime

        public System.DateTime? GetNextExecutionTime(System.DateTime time, System.DateTime? lastExecutionTime)
        {
            return Sidvall.WorkItemManager.SystemContextConfig.Current.EntityFacade.ScheduleManager.GetNextExecutionTime(this, time, lastExecutionTime);
        }

        #endregion
        #region IsExecutionTime

        public bool IsExecutionTime(System.DateTime time, System.DateTime? lastExecutionTime)
        {
            return Sidvall.WorkItemManager.SystemContextConfig.Current.EntityFacade.ScheduleManager.IsExecutionTime(this, time, lastExecutionTime);
        }

        #endregion

        #region Deserialize

        public static Schedule Deserialize(string value)
        {
            Sidvall.WorkItemManager.Net.Services.V1.Schedule dataItem;
            Schedule item;

            dataItem = null;
            try
            {
                if (!string.IsNullOrWhiteSpace(value))
                    dataItem = Sidvall.Serialization.SerializerManager.Current.Deserialize<Sidvall.WorkItemManager.Net.Services.V1.Schedule>(value);
            }
            catch
            {
            }
            if (dataItem == null)
            {
                dataItem = new Net.Services.V1.Schedule();
                dataItem.StartTime = System.DateTime.Today;
            }
            item = GetItem(dataItem);
            item.AcceptChanges();

            return item;
        }

        #endregion
        #region GetItem

        public static Schedule GetItem(Sidvall.WorkItemManager.Data.ISchedule dataItem)
        {
            if (dataItem == null)
                return null;
            var item = new Schedule();
            item.Import(dataItem, Sidvall.Data.CopySettings.FullCopyAll(Sidvall.Data.DataRowContextOption.None, null));
            item.InitializeRules();

            return item;
        }

        #endregion
        #region Serialize

        public static string Serialize(Sidvall.WorkItemManager.Data.ISchedule item)
        {
            Sidvall.WorkItemManager.Net.Services.V1.Schedule dataItem;

            dataItem = new Net.Services.V1.Schedule();
            dataItem.Import(item, Sidvall.Data.CopySettings.FullCopyAll(Sidvall.Data.DataRowContextOption.None, null));

            return Sidvall.Serialization.SerializerManager.Current.SerializeObject(dataItem);
        }

        #endregion

        #region OnPropertyChanged

        protected override void OnPropertyChanged(System.ComponentModel.PropertyChangedEventArgs args, bool raisePropertyChanged, bool updateState)
        {
            base.OnPropertyChanged(args, raisePropertyChanged, updateState);
            if (args == null)
                return;
            if (!string.IsNullOrWhiteSpace(args.PropertyName))
            {
                switch (args.PropertyName)
                {
                    case "StartTime":
                    case "EndDate":
                    case "DayStartTime":
                    case "DayEndTime":
                    case "ScheduleFrequencyTypeId":
                    case "ScheduleFrequency":
                    case "MonthFrequencyTypeId":
                    case "MonthFrequency":
                    case "DayFrequencyTypeId":
                    case "DayFrequency":
                    case "Monday":
                    case "Tuesday":
                    case "Wednesday":
                    case "Thursday":
                    case "Friday":
                    case "Saturday":
                    case "Sunday":
                        InitializeRules();
                        OnPropertyChanged("Description", true, false);
                        OnPropertyChanged("NextExecutionTime", true, false);
                        break;
                    default:
                        break;
                }
            }
        }

        #endregion

        #endregion
        #region Private Members

        #region InitializeRules

        private void InitializeRules()
        {
            switch (this.ScheduleFrequencyTypeId)
            {
                case Sidvall.WorkItemManager.Workflow.Conditions.ScheduleFrequencyType.Monthly:
                    this.MaxScheduleFrequency = 99;
                    break;
                case Sidvall.WorkItemManager.Workflow.Conditions.ScheduleFrequencyType.Weekly:
                    this.MaxScheduleFrequency = 52;
                    break;
                case Sidvall.WorkItemManager.Workflow.Conditions.ScheduleFrequencyType.Daily:
                    this.MaxScheduleFrequency = 99;
                    break;
                default:
                    break;
            }
            if (this.ScheduleFrequency > this.MaxScheduleFrequency)
                this.ScheduleFrequency = this.MaxScheduleFrequency;

            switch (this.MonthFrequencyTypeId)
            {
                case Sidvall.WorkItemManager.Workflow.Conditions.MonthFrequencyType.Day:
                    this.MaxMonthFrequency = 31;
                    break;
                default:
                    this.MaxMonthFrequency = 5;
                    break;
            }
            if (this.MonthFrequency > this.MaxMonthFrequency)
                this.MonthFrequency = this.MaxMonthFrequency;

            switch (this.DayFrequencyTypeId)
            {
                case Sidvall.WorkItemManager.Workflow.Conditions.DayFrequencyType.EveryHour:
                    this.MaxDayFrequency = 24;
                    break;
                case Sidvall.WorkItemManager.Workflow.Conditions.DayFrequencyType.EveryMinute:
                    this.MaxDayFrequency = 60;
                    break;
                case Sidvall.WorkItemManager.Workflow.Conditions.DayFrequencyType.EverySecond:
                    this.MaxDayFrequency = 60;
                    break;
                default:
                    break;
            }
            if (this.DayFrequency > this.MaxDayFrequency)
                this.DayFrequency = this.MaxDayFrequency;
        }

        #endregion

        #endregion
    }
}